<template>
  <div>
    <p class="pinformation" style="margin-top: 7px">搜索条件</p>
    <el-form label-width="100px" :model="queryParams" ref="queryRef">
      <el-row>
        <el-col :span="4">
          <el-form-item label="检测类别：" prop="jclbId">
            <el-select v-model="queryParams.jclbId" clearable>
              <el-option v-for="item in proxy.addListNoLimit(
                jclbList,
                'jclbId',
                'jclbName'
              )" :key="item.jclbId" :value="item.jclbId" :label="item.jclbName" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="点位名称：" prop="pointName">
            <el-input v-model="queryParams.pointName" clearable maxlength="100" @keyup.enter="handleQuery"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="连测天数：" prop="sampleDay">
            <el-input v-model="queryParams.sampleDay" clearable maxlength="100"
              oninput="value=value.replace(/[^0-9]/g,'')" @blur="queryParams.sampleDay = $event.target.value"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="每天频次：" prop="sampleCount">
            <el-input v-model="queryParams.sampleCount" clearable maxlength="100"
              oninput="value=value.replace(/[^0-9]/g,'')"
              @blur="queryParams.sampleCount = $event.target.value"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="是否分包：" prop="isPackage">
            <el-select clearable v-model="queryParams.isPackage">
              <el-option v-for="dict in isPackageList" :key="dict.value" :label="dict.label"
                :value="parseInt(dict.value)" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-row justify="end">
            <el-button type="success" @click="handleQuery">搜索</el-button>
            <el-button class="business resetBtn" @click="resetQuery">重置</el-button>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
    <el-row style="margin-bottom: 10px; justify-content: space-between">
      <el-row>
        <p class="pinformation" style="margin-top: 10px">样品列表</p>
        <p class="totalStyle" style="margin-top: 7px">共 {{ total }} 条数据</p>
      </el-row>
      <el-row justify="space-between">
        <el-button type="success" @click="externalClick(0)">批量添加平行</el-button>
        <el-button type="success" @click="externalClick(11)">批量添加全程序空白</el-button>
        <el-button type="success" @click="externalClick(12)">批量添加运输空白</el-button>
        <el-button type="success" @click="externalClick(19)">批量添加穿透</el-button>
        <el-button type="danger" @click="allDelClick">删除全部质控</el-button>
        <el-button type="danger" @click="delClick('')">批量删除质控</el-button>
      </el-row>
      <el-row></el-row>
    </el-row>
    <el-table ref="mainTableRef" :data="projectList" row-key="sFId" :tree-props="{ children: 'fixerList' }"
      default-expand-all @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column prop="jclbId" label="检测类别" align="center" width="130" fixed show-overflow-tooltip>
        <template #default="scope">
          {{
            scope.row.jclbName
          }}
        </template>
      </el-table-column>
      <el-table-column prop="pointName" label="点位名称" align="center" fixed width="150" show-overflow-tooltip />
      <el-table-column prop="sampleCode" label="样品编号" align="center" width="205" fixed show-overflow-tooltip />
      <el-table-column prop="jcxmNames" label="检测项目" align="center" width="300" show-overflow-tooltip fixed>
        <template #default="scope">
          <span v-html="transformation(scope.row.jcxmNames)"></span>
        </template>
      </el-table-column>
      <el-table-column prop="sampleDay" label="连测天数" align="center" width="85" />
      <el-table-column prop="sampleCount" label="每天频次" align="center" width="85" />
      <el-table-column prop="" label="特别说明" align="center" width="200" show-overflow-tooltip />
      <el-table-column label="是否分包" align="center" width="85">
        <template #default="scope">
          {{
            scope.row.sampleType != -1 ? "" : (scope.row.isPackage == 0 ? "否" : "是")
          }}
        </template>
      </el-table-column>
      <el-table-column prop="qc1" label="平行样要求" align="center" width="300" show-overflow-tooltip />
      <el-table-column prop="qc2" label="全程序空白要求" align="center" width="300" show-overflow-tooltip />
      <el-table-column prop="qc3" label="运输空白要求" align="center" width="300" show-overflow-tooltip />
      <el-table-column prop="saveDesc" label="保存要求" align="center" width="300" show-overflow-tooltip />
      <el-table-column prop="saveTime" label="保存时长" align="center" width="100" />
      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template #default="scope">
          <el-button link type="success" @click="externalClick(0, scope.row)"
            v-if="scope.row.sampleType === -1">外部平行</el-button>
          <el-button link type="success" @click="externalClick(11, scope.row)"
            v-if="scope.row.sampleType === -1">全程序空白</el-button>
          <el-button link type="success" @click="externalClick(12, scope.row)"
            v-if="scope.row.sampleType === -1">运输空白</el-button>
          <el-button link type="success" @click="externalClick(19, scope.row)"
            v-if="scope.row.sampleType === -1">穿透</el-button>
          <el-button link type="success" @click="delClick(scope.row)" v-else>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script setup>
import { getQcSampleList, postQc, delQc } from "@/api/cyCheck/cyReady";
import { projectListJclb } from "@/api/store/jclb";
const { proxy } = getCurrentInstance();
const { sys_degree } = proxy.useDict("sys_degree");
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  jclbId: -1,
  isPackage: -1,
});
const dateRange = ref([]);
const total = ref(0);
const projectList = ref([]);
const jclbList = ref([]);
const sFIds = ref([]);
const isPackageList = ref([
  { value: -1, label: "不限" },
  { value: 0, label: "否" },
  { value: 1, label: "是" },
]);
const props = defineProps({
  projectId: {
    type: String, //参数类型
  },
});

function getId() {
  nextTick(() => {
    queryParams.value.projectId = props.projectId;
    getList();
  });
}

function getList() {
  getQcSampleList(queryParams.value).then((res) => {
    projectList.value = res.rows;
    total.value = res.total;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

const delIds = ref([]);
function handleSelectionChange(selection) {
  sFIds.value = [];
  delIds.value = [];
  selection.forEach((item) => {
    if (item.sampleType === -1) {
      sFIds.value.push(item.sFId);
    } else {
      delIds.value.push(item.sFId);
    }
  });
}

function externalClick(id, row) {
  let tempSFIds = row ? [row.sFId] : sFIds.value;
  if (!tempSFIds.length) {
    proxy.$modal.msgError("请选择原样！");
    return false;
  }
  postQc(id, { sFIds: tempSFIds }).then((res) => {
    getList();
    proxy.$modal.msgSuccess("保存成功");
  });
}

function delClick(row) {
  let tempSFIds = row ? [row.sFId] : delIds.value;
  if (!tempSFIds.length) {
    proxy.$modal.msgError("请选择质控样品！");
    return false;
  }
  proxy.$modal
    .confirm("是否确定删除？")
    .then(() => {
      delQc(props.projectId, { sFIds: tempSFIds }).then((res) => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      });
    })
    .catch(() => { });
}

function allDelClick() {
  proxy.$modal
    .confirm("是否确定全部删除？")
    .then(() => {
      delQc(props.projectId, { sFIds: [500] }).then((res) => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      });
    })
    .catch(() => { });
}

getId();

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 22 }];
    }
    getTableHeight();
  }
);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);
function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}

projectListJclb({ projectId: props.projectId }).then((res) => {
  jclbList.value = res.data;
});
</script>

<style></style>
